﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Data;
using System.Windows.Input;
using Client.Commands;
using Contracts.Entities;
using Client.Resources;

namespace Client.ViewModels.WebImport
{
    public class SearchResultsViewModel : WizardPageViewModel
    {
        private List<SearchResultViewModel> displayItems;
        private IEnumerable<MovieItem> items;

        public SearchResultsViewModel(IEnumerable<MovieItem> items)
        {
            this.items = items;
            this.displayItems = new List<SearchResultViewModel>();
        }

        public ListCollectionView Items { get; set; }

        public ICommand AssignSearchResultCommand 
        {
            get 
            {
                return new RelayCommand<MovieItem>((m) => AssignMovie(m));
            }
        }

        /// <summary>
        /// Film wird vom Benutzer zugewiesen
        /// </summary>
        private void AssignMovie(MovieItem result)
        {
            var group = displayItems.Where(m => m.Movie.Name == result.Name);

            foreach (var item in group)
            {
                var movie = item.Movie;

                if (movie.TmdbId == result.TmdbId)
                {
                    if (item.IsChecked)
                        item.IsChecked = true;
                    else
                        item.IsChecked = false;

                    OnResult(movie);
                    continue;
                }

                item.IsChecked = false;   
            }
        }

        #region WebImportBaseViewModel Member

        public override bool IsValid()
        {
            return displayItems.Any(m => m.IsChecked);
        }

        public override string GetButtonNextContent()
        {
            return "Weiter >>";
        }

        /// <summary>
        /// Suchergebnisse laden und gefilterte vorselektieren:
        /// 1. Bei 1 Suchtreffer
        /// </summary>
        public override void Process(object data)
        {
            var results = data as IEnumerable<TmdbSearchResult>;

            foreach (var result in results)
            {
                foreach (var item in result.Items)
                {
                    var viewModel = new SearchResultViewModel();
                    viewModel.Movie = item;

                    // Filme mit einem Ergebnis werden der Importmenge hinzugefügt
                    if (result.Items.Count() == 1)
                    {
                        viewModel.IsChecked = true;
                        OnResult(viewModel.Movie);
                    }

                    displayItems.Add(viewModel);

                 
                }
            }

            var collection = new ListCollectionView(displayItems as IList);
            collection.GroupDescriptions.Add(new PropertyGroupDescription("Movie.Name"));

            Items = collection;
            NotifyPropertyChanged("Items");                 
        }

        public override event Action<object> OnResult;

        #endregion        
                   
    }
}
